package service.impl;

import dao.RecruiterDao;
import domain.Recruiter;
import service.RecruiterService;
import service.RegisterService;
import util.CheckUtil;
import util.MyBatisUtil;
import util.PrintUtil;

import java.util.List;

public final class RecruiterServiceImpl implements RecruiterService, RegisterService {
    private RecruiterServiceImpl() {}
    private static final RecruiterServiceImpl INSTANCE = new RecruiterServiceImpl();
    private static final RecruiterDao recruiterDao = MyBatisUtil.getMapper(RecruiterDao.class);
    public static RecruiterServiceImpl getINSTANCE() { return INSTANCE; }

    @Override
    public int registerRecruiter(Recruiter recruiter) {
        return recruiterDao.insert(recruiter);
    }

    @Override
    public int resetRecruiter(Recruiter recruiter) {
        return recruiterDao.update(recruiter);
    }

    @Override
    public Recruiter login(String tel, String password) {
        Recruiter recruiter = recruiterDao.selectByTel(tel);
        if(recruiter!=null&&recruiter.getPassword().equals(password)){
            return recruiter;
        }
        return null;
    }

    @Override
    public Recruiter selectById(Long id) {
        return recruiterDao.selectById(id);
    }

    @Override
    public Recruiter selectByTel(String tel) {
        return recruiterDao.selectByTel(tel);
    }

    @Override
    public int verifyRegisterTel(String tel) {
        if(!CheckUtil.checkTel(tel)){
            return 1;
        }
        if(selectByTel(tel)!=null){
            return 2;
        }
        return 0;
    }

    @Override
    public int verifyRegisterName(String name) {
        if(CheckUtil.checkName(name)){
            return 1;
        }
        if(recruiterDao.selectByName(name)!=null){
            return 2;
        }
        return 0;
    }

    @Override
    public List<Recruiter> selectAll() {
        return recruiterDao.selectAll();
    }
}
